package sp.whereru;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.TextView;

public class ViewCourseActivity extends FragmentActivity {
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_course);
		
		CourseDbAdapter mDbHelper = new CourseDbAdapter(this);
		mDbHelper.open();
		
		Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        
        long courseId = extras.getLong(CourseScheduleActivity.EXTRA_COURSE_ID);
        Cursor c = mDbHelper.fetchCourse(courseId);
        
        String courseName = c.getString(c.getColumnIndex(CourseDbAdapter.KEY_NAME));
        String building = c.getString(c.getColumnIndex(CourseDbAdapter.KEY_BUILDING));
        String room = c.getString(c.getColumnIndex(CourseDbAdapter.KEY_ROOM));
        int startTime = c.getInt(c.getColumnIndex(CourseDbAdapter.KEY_START));
        int endTime = c.getInt(c.getColumnIndex(CourseDbAdapter.KEY_END));
        String days = c.getString(c.getColumnIndex(CourseDbAdapter.KEY_DAYS));
        int alarm = c.getInt(c.getColumnIndex(CourseDbAdapter.KEY_ALARM));
        int alarmDelay = c.getInt(c.getColumnIndex(CourseDbAdapter.KEY_ALARM_DELAY));
        
        String sAlarm;
        String sAlarmDelay;
        
        if(alarm == 0)
        	sAlarm = "No alarm set";
        else
        	sAlarm = "Alarm is set ";
        
        if(alarmDelay == 0)
        	sAlarmDelay = "";
        else
        	sAlarmDelay = alarmDelay + " minutes before class";
        	
        
        SimpleTime start = new SimpleTime(startTime);
        SimpleTime end = new SimpleTime(endTime);
        
        TextView vName = (TextView) findViewById(R.id.viewCourse_name);
        TextView vBuilding = (TextView) findViewById(R.id.viewCourse_building);
        TextView vRoom = (TextView) findViewById(R.id.viewCourse_room);
        TextView vStartTime = (TextView) findViewById(R.id.viewCourse_startTime);
        TextView vEndTime = (TextView) findViewById(R.id.viewCourse_endTime);
        TextView vDays = (TextView) findViewById(R.id.viewCourse_days);
        TextView vAlarm = (TextView) findViewById(R.id.viewCourse_alarmIsSet);
        TextView vAlarmDelay = (TextView) findViewById(R.id.viewCourse_alarmDelay);
        
        vName.setText(courseName);        
        vBuilding.setText(building + " ");
        vRoom.setText(room);
        vStartTime.setText(start.toString() + " - ");
        vEndTime.setText(end.toString());
        vDays.setText(days);
        vAlarm.setText(sAlarm);
        vAlarmDelay.setText(sAlarmDelay);        
        
        mDbHelper.close();
	}
}
